<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

class Letter extends Model
{
    protected $table = "letter";
    protected $fillable = ['username', 'email', 'title', 'content', 'send_time', 'is_public',];

    protected $hidden = ['created_at'];


    public static function checkValidate($data){
        $tomorrow = Carbon::tomorrow()->toDateString();
        $rules = [
            'username'  => 'required',
            'email' => 'required|email',
            'send_time' => 'required|date_format:Y-m-d H:i|after_or_equal:'.$tomorrow,
            'content' => 'required|min:50',
        ];
        $message = [
            'username.required'  => '寄信人姓名不能为空',
            'email.required' => '收信邮箱不能为空且必须真实存在',
            'email.email' => '收信邮箱必须为真实存在的邮箱地址',
            'send_time.required' => '发信时间不能为空，否则不能按时发送邮件',
            'send_time.date_format' => '发信时间格式错误 示例：2025-12-25 08:00',
            'send_time.after_or_equal' => '发信时间要求必须24小时以后',
            'content.required' => '信件正文不能为空',
            'content.min' => '信件正文不能少于50字符',

        ];
        $validator = Validator::make($data,$rules,$message);
        return $validator;
    }
}
